package servlet;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import pojo.User;
import dao.UserDao;
import dao.impl.UserDaoImpl;

public class AvatarServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	private UserDao userDao;
    public AvatarServlet() {
        super();
       userDao = new UserDaoImpl();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String id = request.getParameter("uid");
		if (id!=null&&!"null".equals(id)&&!"".equals(id)) {
			User user = userDao.loadUser(Integer.valueOf(id));
			if (user.getAvatar()!=null) {
				FileInputStream in = new FileInputStream(user.getAvatar());
				OutputStream out = response.getOutputStream();
				byte[] buf = new byte[512];
				int len = 0;
				while ((len=in.read(buf))!=-1) {
					out.write(buf,0,len);
				}
				in.close();
				out.close();
			}
			
		}
		
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
